package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ArticleQueryDTO;
import com.jeff.tianti.cms.dto.ColumnInfoDTO;
import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.entity.Article;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.service.ArticleService;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.org.dto.DictionaryDTO;
import com.jeff.tianti.org.entity.Dictionary;
import com.jeff.tianti.org.service.DictionaryService;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * CMS的Controller
 *
 * @author Jeff Xu
 */
@Controller
@RequestMapping("/dic")
public class DictionaryController {

    @Autowired
    private ColumnInfoService columnInfoService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private ArticleService articleService;

    /**
     * 获取字典列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String columnList(HttpServletRequest request, Model model) {
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String rootDicId = request.getParameter("rootDicId");

        DictionaryDTO dicDTO = new DictionaryDTO();
        dicDTO.setLevel(Dictionary.LEVEL_ROOT);
        List<Dictionary> rootDictionaryList = this.dictionaryService.queryDicList(dicDTO);
        if (StringUtils.isBlank(rootDicId) && rootDictionaryList != null && rootDictionaryList.size() > 0) {
            rootDicId = rootDictionaryList.get(0).getId();
        }
        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        dictionaryDTO.setPid(rootDicId);
        dictionaryDTO.setCodeLike(true);
        dictionaryDTO.setCode(code);
        dictionaryDTO.setName(name);
        List<Dictionary> list = this.dictionaryService.queryDicList(dictionaryDTO);

        model.addAttribute("list", list);
        model.addAttribute("dictionaryDTO", dictionaryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_DICTIONARY_LIST);
        model.addAttribute("rootDictionaryList", rootDictionaryList);

        return "/cms/dic/dic_list";
    }

    /**
     * 校验字典编码是否存在
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/validator/code")
    @ResponseBody
    public Map<String, Object> ajaxValidatorUsername(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        String code = request.getParameter("code");
        String id = request.getParameter("id");
        List<Dictionary> dictionaries = this.dictionaryService.validatorCode(code,id);
        if (dictionaries != null && dictionaries.size() > 0) {
            map.put("error", "编码已经存在");
        } else {
            map.put("ok", "");
        }
        return map;
    }

    /**
     * 字典修改
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model) {
        Dictionary dictionary = null;
        String id = request.getParameter("id");
        String level = request.getParameter("level");
        String rootDicId = request.getParameter("rootDicId");
        String channel = request.getParameter("channel");
        DictionaryDTO dicDTO = new DictionaryDTO();
        dicDTO.setLevel(Dictionary.LEVEL_ROOT);
        List<Dictionary> rootDictionaryList = this.dictionaryService.queryDicList(dicDTO);
        if (StringUtils.isNotBlank(id)) {
            dictionary = this.dictionaryService.find(id);
        }
        model.addAttribute("dictionary", dictionary);
        model.addAttribute("level", level);
        model.addAttribute("rootDicId", rootDicId);
        model.addAttribute("channel", channel);
        model.addAttribute("rootDictionaryList", rootDictionaryList);
        return "/cms/dic/dialog/dic_edit";
    }

    /**
     * 字典保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doColumnajaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String level = request.getParameter("level");
            String pid = request.getParameter("pid");
            String channel = request.getParameter("channel");

            Dictionary dictionary = null;
            dictionary = new Dictionary();
            dictionary.setId(id);
            dictionary.setCode(code);
            dictionary.setName(name);
            dictionary.setDeleteFlag(Dictionary.DELETE_FLAG_NORMAL);
            dictionary.setLevel(level);
            dictionary.setPid(pid);
            dictionary.setChannel(channel);

            if (!StringUtils.isNotBlank(id)) {
                dictionaryService.save(dictionary);
            } else {
                dictionaryService.updateSelective(dictionary);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 字典删除
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/update/deleteFlage")
    @ResponseBody
    public AjaxResult ajaxUpdColumnDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            String id = (String) request.getParameter("id");
            String deleteFlag = request.getParameter("deleteFlag");
            Dictionary dictionary = new Dictionary();
            dictionary.setId(id);
            dictionary.setDeleteFlag(deleteFlag);
            dictionaryService.updateSelective(dictionary);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

}
